On the Optimality of a Family of Binary Trees

نویسندگان

  • Dana Vrajitoru
  • William Knight
چکیده

In this technical report we present an analysis of the complexity of a class of algorithms. These algorithms recursively explore a binary tree and need to make two recursive calls for one of the subtrees and only one for the other. We derive the complexity of these algorithms in the worst and in the best case and show the tree structures for which these cases happen. 1 The Problem Let us consider a traversal function for an arbitrary binary tree. Most of these functions are recursive, although an iterative version is not too difficult to implement with the use of a stack. The object of this technical report, though, is those functions that are recursive. For the remainder of the paper we’ll consider the classic C++ implementation of a tree node as follows: template struct node { otype datum; node *left, *right; }; When a recursive function makes a simple traversal of a binary tree in which the body of the traversal function contains exactly two recursive calls, one on the pointer to the left subtree and one on the pointer to the right, and all other parts of each call, exclusive of the recursive calls, require time bounded by constants, then the execution time for traversal of a tree with n nodes is roughly proportional to the total number of calls (initial and recursive) that are made. In this case that will be 1+2n (the call on the pointer to the root of the tree and one call on each of the 2n pointers in the tree), so the execution time is Θ(n). The analysis would apply, for example, to the function in Figure 1 that traverses the tree to calculate its height. Figure 2 shows a differently coded version of the function that calculates the height of a binary tree. Note that the code is a little simpler (shorter) than the code in the version in Figure 1. The code in Figure int height (node_ptr p) // p is a pointer to a binary tree { if (p == NULL) return -1; // The base case of an empty binary tree. int left_height = height (p->left); int right_height = height (p->right); if (left_height <= right_height) return 1 + right_height; else return 1 + left_height; } Figure 1: The height of a binary tree 2 is not a “simple traversal” of the kind described above. Here is the reason: when recursive calls are made, exactly one of the recursive calls is repeated. Clearly then the total number of calls (initial and recursive) is not just 2n+ 1, where n is the number of nodes in the tree. We shall try to figure out the total number of calls that could be made when the second version of height is called on a tree T with n nodes. int height (node_ptr p) // p is a pointer to a binary tree { if (p == NULL) return -1; // The base case of an empty binary tree. if (height(p->left) <= height(p->right)) return 1 + height(p->right); else return 1 + height(p->left); } Figure 2: Inefficient version of the function height At first sight it would seem that this is not a very useful problem to study because we can easily correct the fact that this function performs two recursive calls on one of the subtrees. We can store the result of the function in a local variable and use it instead of the second recursive call, as shown in Figure 1. Even if this is the case indeed, it would still be useful to know just “how bad” the complexity of the function can get from a simple change. The second motivation is that just as the function in Figure 1 is representative of a whole class of traversal functions for binary trees, the analysis for the function in Figure 2 can also be applied to a whole class of functions. Some of these can be optimized with the method used for the function height, but some of them might require operations making the second recursive call on the same subtree necessary.

برای دانلود متن کامل این مقاله و بیش از 32 میلیون مقاله دیگر ابتدا ثبت نام کنید

ثبت نام

اگر عضو سایت هستید لطفا وارد حساب کاربری خود شوید

منابع مشابه

A New Heuristic Algorithm for Drawing Binary Trees within Arbitrary Polygons Based on Center of Gravity

Graphs have enormous usage in software engineering, network and electrical engineering. In fact graphs drawing is a geometrically representation of information. Among graphs, trees are concentrated because of their ability in hierarchical extension as well as processing VLSI circuit. Many algorithms have been proposed for drawing binary trees within polygons. However these algorithms generate b...

متن کامل

Profile and Height of Random Binary Search Trees

The purpose of this article is to survey recent results on distributional properties of random binary search trees. In particular we consider the profile and the height.

متن کامل

P´olya Urn Models and Connections to Random Trees: A Review

This paper reviews P´olya urn models and their connection to random trees. Basic results are presented, together with proofs that underly the historical evolution of the accompanying thought process. Extensions and generalizations are given according to chronology: • P´olya-Eggenberger’s urn • Bernard Friedman’s urn • Generalized P´olya urns • Extended urn schemes • Invertible urn schemes ...

متن کامل

Probabilistic analysis of the asymmetric digital search trees

In this paper, by applying three functional operators the previous results on the (Poisson) variance of the external profile in digital search trees will be improved. We study the profile built over $n$ binary strings generated by a memoryless source with unequal probabilities of symbols and use a combinatorial approach for studying the Poissonized variance, since the probability distribution o...

متن کامل

Just chromatic exellence in fuzzy graphs

A fuzzy graph is a symmetric binary fuzzy relation on a fuzzy subset. The concept of fuzzy sets and fuzzy relations was introduced by L.A.Zadeh in 1965cite{zl} and further studiedcite{ka}. It was Rosenfeldcite{ra} who considered fuzzy relations on fuzzy sets and developed the theory of fuzzy graphs in 1975. The concepts of fuzzy trees, blocks, bridges and cut nodes in fuzzy graph has been studi...

متن کامل

A Study on Splay Trees

We study the dynamic optimality conjecture, which predicts that splay trees are a form of universally efficient binary search tree, for any access sequence. We reduce this claim to a regular access bound, which seems plausible and might be easier to prove. This approach may be useful to establish dynamic optimality.

متن کامل

ذخیره در منابع من


  با ذخیره ی این منبع در منابع من، دسترسی به آن را برای استفاده های بعدی آسان تر کنید

برای دانلود متن کامل این مقاله و بیش از 32 میلیون مقاله دیگر ابتدا ثبت نام کنید

ثبت نام

اگر عضو سایت هستید لطفا وارد حساب کاربری خود شوید

عنوان ژورنال:

دوره   شماره 

صفحات  -

تاریخ انتشار 2011